package com.gxuwz.bignews.model.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.gxuwz.bignews.model.entity.User;

public class UserDao {
    private com.gxuwz.bignews.db.DatabaseHelper dbHelper;

    public UserDao(Context context) {
        dbHelper = new com.gxuwz.bignews.db.DatabaseHelper(context);
    }

    public User getUserByPhoneAndPassword(String phone, String password) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(com.gxuwz.bignews.db.DatabaseHelper.TABLE_USERS,
                new String[]{com.gxuwz.bignews.db.DatabaseHelper.COLUMN_PHONE, com.gxuwz.bignews.db.DatabaseHelper.COLUMN_USERNAME, com.gxuwz.bignews.db.DatabaseHelper.COLUMN_PASSWORD, com.gxuwz.bignews.db.DatabaseHelper.COLUMN_AVATAR},
                com.gxuwz.bignews.db.DatabaseHelper.COLUMN_PHONE + "=? AND " + com.gxuwz.bignews.db.DatabaseHelper.COLUMN_PASSWORD + "=?",
                new String[]{phone, password},
                null, null, null);

        User user = null;
        if (cursor != null && cursor.moveToFirst()) {
            String username = cursor.getString(cursor.getColumnIndexOrThrow(com.gxuwz.bignews.db.DatabaseHelper.COLUMN_USERNAME));
            String avatar = cursor.getString(cursor.getColumnIndexOrThrow(com.gxuwz.bignews.db.DatabaseHelper.COLUMN_AVATAR));
            user = new User(phone, username, password, avatar);
            cursor.close();
        }
        db.close();
        return user;
    }

    public boolean isPhoneRegistered(String phone) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(com.gxuwz.bignews.db.DatabaseHelper.TABLE_USERS,
                new String[]{com.gxuwz.bignews.db.DatabaseHelper.COLUMN_PHONE},
                com.gxuwz.bignews.db.DatabaseHelper.COLUMN_PHONE + "=?",
                new String[]{phone},
                null, null, null);
        boolean exists = (cursor != null && cursor.moveToFirst());
        if (cursor != null) cursor.close();
        db.close();
        return exists;
    }

    public boolean registerUser(User user) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(com.gxuwz.bignews.db.DatabaseHelper.COLUMN_PHONE, user.getPhone());
        values.put(com.gxuwz.bignews.db.DatabaseHelper.COLUMN_PASSWORD, user.getPassword());
        values.put(com.gxuwz.bignews.db.DatabaseHelper.COLUMN_USERNAME, user.getUsername());
        values.put(com.gxuwz.bignews.db.DatabaseHelper.COLUMN_AVATAR, user.getAvatar());
        long result = db.insert(com.gxuwz.bignews.db.DatabaseHelper.TABLE_USERS, null, values);
        db.close();
        return result != -1;
    }

    public boolean updatePassword(String phone, String newPassword) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(com.gxuwz.bignews.db.DatabaseHelper.COLUMN_PASSWORD, newPassword);
        int rowsAffected = db.update(com.gxuwz.bignews.db.DatabaseHelper.TABLE_USERS, values, com.gxuwz.bignews.db.DatabaseHelper.COLUMN_PHONE + "=?", new String[]{phone});
        db.close();
        return rowsAffected > 0;
    }

    public boolean updateAvatar(String phone, String avatar) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(com.gxuwz.bignews.db.DatabaseHelper.COLUMN_AVATAR, avatar);
        int rowsAffected = db.update(com.gxuwz.bignews.db.DatabaseHelper.TABLE_USERS, values, com.gxuwz.bignews.db.DatabaseHelper.COLUMN_PHONE + "=?", new String[]{phone});
        db.close();
        return rowsAffected > 0;
    }

    public boolean updateUsername(String phone, String newUsername) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(com.gxuwz.bignews.db.DatabaseHelper.COLUMN_USERNAME, newUsername);
        int rowsAffected = db.update(com.gxuwz.bignews.db.DatabaseHelper.TABLE_USERS, values, com.gxuwz.bignews.db.DatabaseHelper.COLUMN_PHONE + "=?", new String[]{phone});
        db.close();
        return rowsAffected > 0;
    }
} 